<?php

namespace app\controllers;
use yii\db\Query;
use yii\web\Controller;


class ApiArticleController extends ApiBaseController {
     //求职助手

    //求职助手列表
    public function actionIndex(){
        $list=(new Query())->select('id,title,desc,cover')->from('yii_article')->orderBy('create_time desc')->all();
       foreach ($list as $k=>$v){
           $list[$k]['cover']=$this->GetImg($v['cover']);
       }
        return json_encode($list);
    }

    //查询求职助手详情
    public function actionLook($id){
        $where['id']=(int)$id;
        $list=(new Query())->select('id,title,author,create_time,content,view,cover')->from('yii_article')->where($where)->one();
        $list['create_time']=date('Y-m-d',$list['create_time']);
        $list['cover']=$this->GetImg($list['cover']);
        $list['next']=$this->TwoArticle($list['id'],1);
        $list['last']=$this->TwoArticle($list['id'],2);
        
        $data['view']=$list['view']+1;
      
       \Yii::$app->db->createCommand()->update('yii_article',$data, $where)->execute();
        return json_encode($list);
    }

    //获取上下条求助助手
    public function TwoArticle($id,$type){
        switch ($type){
            case 1:
                $id--;
                break;
            case 2:
                $id++;
                break;
        }
        $list=(new Query())->select('id,title')->from('yii_article')->where(['id'=>$id])->one();
        return $list;
    }



}
